import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/goods_info_common.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/store_order_controller/conversion_picking_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:common_utils/common_utils.dart';

class ConversionPickingPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ConversionPickingPageController>(
      init: ConversionPickingPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: '转提货',
            backgroundColor: pageBgColor,
          ),
          body: GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: ScrollConfigurationCommon(child: ListView(
              children: [
                Obx(() => Container(
                  padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 12.h),
                  color: bgWhiteColor,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '商品信息',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp,
                              fontWeight: FontWeight.bold
                            ),
                          ),
                          Text(
                            controller.statusStr.value,
                            style: TextStyle(
                              color: mainTextColor,
                              fontSize: 14.sp
                            ),
                          ),
                        ],
                      ),
                      SizedBox(height: 12.h),
                      Column(
                        children: controller.products.map((productItem) {
                          return GoodsInfoCommon(
                            title: productItem['title'], 
                            headImage: productItem['image'],
                            price: productItem['price'],
                            quantity: productItem['quantity']
                          );
                        }).toList(),
                      )
                    ],
                  ),
                )),
                Container(
                  margin: EdgeInsets.only(top: 12.h),
                  padding: EdgeInsets.all(16.w),
                  color: bgWhiteColor,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '请输入接受人手机号',
                        style: TextStyle(
                          color: textBlackColor,
                          fontSize: 14.sp
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.only(left: 12.w),
                        margin: EdgeInsets.only(top: 12.h),
                        height: 44.h,
                        decoration: BoxDecoration(
                          border: Border.all(
                            color: lineColor,
                            width: 1
                          ),
                          borderRadius: BorderRadius.circular(5.w)
                        ),
                        child: Row(
                          children: [
                            Expanded(child: TextField(
                              textAlignVertical: TextAlignVertical.bottom,
                              decoration: InputDecoration(
                                isDense: true,
                                border: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                                enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                                focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                                counterText: '',
                                hintText: '请输入对方手机号',
                                hintStyle: TextStyle(
                                  fontSize: 14.sp,
                                  color: tipsColor,
                                )
                              ),
                              maxLength: 11,
                              keyboardType: TextInputType.number,
                              inputFormatters: [
                                FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                              ],
                              textInputAction: TextInputAction.search,
                              cursorColor: textBlackColor,
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: textBlackColor
                              ),
                              onChanged: (String value) {
                                if (RegexUtil.isMobileSimple(value)) {
                                  controller.mobile.value = value;
                                  controller.getReceiveUserList();
                                  return;
                                }
                              },
                              onSubmitted: (String value) {
                                controller.mobile.value = value;
                                controller.getReceiveUserList();
                              },
                            )),
                            InkWellUtil.shakeProofInkWell(Container(
                              padding: EdgeInsets.symmetric(horizontal: 12.w),
                              child: Image.asset(
                                'assets/icons/icon_search.png'
                              ),
                            ), () {
                              controller.getReceiveUserList();
                            })
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Obx(() => controller.isSearch.value ? Container(
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: bgWhiteColor,
                    border: Border(
                      top: BorderSide(
                        width: .5,
                        color: lineColor
                      )
                    )
                  ),
                  padding: EdgeInsets.all(16.w),
                  child: controller.receiveUserArr.length > 0 ? Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '查找人',
                        style: TextStyle(
                          color: textSubtitleColor,
                          fontSize: 14.sp
                        ),
                      ),
                      SizedBox(height: 12.h),
                      Row(
                        children: [
                          Container(
                            width: 54.w,
                            child: Text(
                              '${controller.receiveUserArr[0].username}',
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 14.sp
                              ),
                            ),
                          ),
                          Text(
                            '${controller.receiveUserArr[0].mobile}',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 14.sp
                            ),
                          )
                        ],
                      )
                    ],
                  ) : Text(
                    '找不到用户，请重新输入',
                    style: TextStyle(
                      color: mainTextColor,
                      fontSize: 14.sp
                    ),
                  ),
                ) : Container())
              ],
            ))
          ),
          bottomNavigationBar: BottomAppBar(
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              height: 56.h,
              color: bgWhiteColor,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Container(
                    width: 120.w,
                    margin: EdgeInsets.only(left: 12.w),
                    child: ButtonUtil(
                      text: '取消', 
                      fontSize: 14.sp, 
                      borderRadius: 36.h, 
                      height: 36.h, 
                      textColor: textSubtitleColor,
                      showBorder: true,
                      borderColor: lineColor,
                      bgColors: [
                        bgWhiteColor,
                        bgWhiteColor
                      ],
                      onTop: () {
                        Get.back();
                      }
                    ),
                  ),
                  Opacity(
                    opacity: controller.receiveUserArr.length > 0 ? 1 : .5,
                    child: Container(
                      width: 120.w,
                      margin: EdgeInsets.only(left: 12.w),
                      child: ButtonUtil(
                        text: '确认', 
                        fontSize: 14.sp, 
                        borderRadius: 36.h, 
                        height: 36.h, 
                        onTop: () {
                          if (controller.receiveUserArr.length > 0) {
                            controller.confirmGift();
                          }
                        }
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        );
      },
    );
  }    
}